/**
 * ListTasksController.java
 * com.burgess.rtd.controller
 *
 * Created Jun 24, 2009
 *
 * Author: Andrew Burgess
 * Email: abombm1@gmail.com
 * Copyright: 2009
 */
package com.burgess.rtd.controller;

import java.util.Calendar;

import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.Cursor;

import com.burgess.rtd.constants.Program;
import com.burgess.rtd.constants.RTM;
import com.burgess.rtd.exceptions.RTDException;
import com.burgess.rtd.interfaces.view.IListTasksView;
import com.burgess.rtd.model.Database;
import com.burgess.rtd.model.List;
import com.burgess.rtd.model.Request;
import com.burgess.rtd.model.Task;
import com.burgess.rtd.service.SyncService;

/**
 *
 */
public class ListTasksController {
	private IListTasksView view;
	private Database dbHelper;
	private String token;
	private long timeline;
	
	public ListTasksController(IListTasksView view) {
		this.view = view;
		
		dbHelper = new Database(view.getContext());
		try {
			dbHelper.open();
		} catch (RTDException e) {
			view.createErrorDialog(e.error);
		}
		
		SharedPreferences prefs = view.getContext().getSharedPreferences(Program.APPLICATION, 0);
		token = prefs.getString(Program.Config.AUTH_TOKEN, Program.DEFAULT_AUTH_TOKEN);
		timeline = prefs.getLong(Program.Config.TIMELINE, 0);
	}
	
	public void initializeView() {
		long id = view.getListId();
		long remoteId = -1;
		Cursor cursor;
		try {
			
			remoteId = List.getRemoteId(dbHelper.getDb(), id);
			
			cursor = dbHelper.getDb().rawQuery("SELECT task_series._id AS _id, task_series.name, tasks.due_date, tasks.has_due_time, tasks.priority " +
					"FROM task_series " +
					"INNER JOIN tasks ON (task_series._id = tasks.task_series_id OR task_series.remote_id = tasks.remote_task_series_id) " +
					"WHERE (list_id = ? OR remote_list_id = ?) AND completed IS NULL " +
					"ORDER BY priority, due_date, name ", new String[] {"" + id, "" + remoteId });
			
			view.setupTaskList(cursor);
		} catch (RTDException e) {
			
		}
	}
	
	public void stop() {
		dbHelper.close();
	}

	public void completeTask(long id) {
		try {
			ContentValues cv = new ContentValues();
			cv.put(Task.COMPLETED, Program.DATE_FORMAT.format(Calendar.getInstance().getTime()));
			dbHelper.getDb().update(Task.TABLE, cv, Task.ID + "=" + id, null);
			
			Request r = new Request(RTM.Tasks.COMPLETE);
			r.setParameter("auth_token", token);
			r.setParameter("timeline", timeline);
			r.setParameter("list_id", List.getRemoteId(dbHelper.getDb(), id));
			
			r.save(dbHelper.getDb(), id, Program.Data.LIST);
			
			SyncService s = new SyncService(view.getContext());
			
			s.sendRequests();
		} catch (RTDException e) {
			
		}
	}

	public void postponeTask(long id) {
		try {
			ContentValues cv = new ContentValues();
			cv.put(List.DELETED, true);
			dbHelper.getDb().update(List.TABLE, cv, List.ID + "=" + id, null);
			
			Request r = new Request(RTM.Lists.DELETE);
			r.setParameter("auth_token", token);
			r.setParameter("timeline", timeline);
			r.setParameter("list_id", List.getRemoteId(dbHelper.getDb(), id));
			
			r.save(dbHelper.getDb(), id, Program.Data.LIST);
			
			SyncService s = new SyncService(view.getContext());
			
			s.sendRequests();
		} catch (RTDException e) {
			
		}
	}
}
